package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectiBillStatusConst;
import com.yonyou.pmclouds.safetyrectification.entity.SafetyRectificationVO;
import com.yonyou.pmclouds.safetyrectification.relationuser.entity.RelationUserVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 安全整改-消息通知构造器
 *
 * @author tanghuan
 * @date 2019/10/27
 */
@Component
public class SafetyRectificationNoticeConstructor extends AbstractMessageNoticeConstructor<SafetyRectificationVO> {
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;

    @Override
    protected MessageNoticeVO[] generateContent(SafetyRectificationVO vo) throws BusinessException {
        if (vo == null)
            return new MessageNoticeVO[0];

        ProjectVO projectVO = projectSimpleQuery.queryProject(vo.getPkProject());

        List<MessageNoticeVO> noticeVOList = new ArrayList<>();
        String messageContent = vo.getCreatorName() + "提交的[安全整改]";

        // 通知内容
        if (SafetyRectificationVO.OPERATION_ADD.equalsIgnoreCase((vo.getOperation()))) {
            // 制单时给抄送人发送通知：xxx提交的【安全整改】抄送了您
            messageContent = messageContent + "抄送了您";
        } else if (SafetyRectificationVO.OPERATION_ASSIGN.equalsIgnoreCase(vo.getOperation())) {
            // 交办提交时给抄送人和制单人发送通知：xxx提交的【安全整改】交办给xxx
            messageContent = messageContent + "交办给" + vo.getRectificationerName();
            // 交办给制单人发送通知
            noticeVOList.add(getNoticeVO(vo, projectVO.getSname(), messageContent, vo.getCreator()));
        } else if (SafetyRectificationVO.OPERATION_RECTIFY.equalsIgnoreCase(vo.getOperation())) {
            // 整改完给抄送人发送通知：xxx提交的【安全整改】已整改
            messageContent = messageContent + "已整改";
        } else if (SafetyRectificationVO.OPERATION_PASS.equalsIgnoreCase(vo.getOperation())) {
            messageContent = messageContent + "已关闭";
        } else if (SafetyRectificationVO.OPERATION_NO_PASS.equalsIgnoreCase(vo.getOperation())) {
            messageContent = messageContent + "验收未通过";
        }

        // 给抄送人发送通知
        List<RelationUserVO> relationUserVOList = vo.getRelationUserList();
        if (CollectionUtils.isNotEmpty(relationUserVOList)) {
            for (RelationUserVO relationUserVO : relationUserVOList) {
                noticeVOList.add(getNoticeVO(vo, projectVO.getSname(), messageContent, relationUserVO.getRelatedUser()));
            }
        }

        // 验收通过给整改人发送通知
        if (vo.getBillStatus() == SafetyRectiBillStatusConst.STATUS_END) {
            messageContent = APPROVAL_PREFIX_MESSAGE + "安全整改已审核通过";
            noticeVOList.add(getNoticeVO(vo, projectVO.getSname(), messageContent, vo.getPkRectificationer()));
        }

        return noticeVOList.toArray(new MessageNoticeVO[0]);
    }

    /**
     * 生成一条消息通知
     *
     * @param vo
     * @param title
     * @param content
     * @param receiver
     * @return
     */
    private MessageNoticeVO getNoticeVO(SafetyRectificationVO vo, String title, String content, String receiver) {
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(vo.getBillType());
        noticeVO.setRelateKey(vo.getPkSafetyRectification());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(vo.getPkTenant());
        noticeVO.setMessageContent(content);

        return noticeVO;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE;
    }
}
